package com.aim.Activity;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.aim.api.AIMCaseConstants;

public class ShowPhotos extends Activity{
	Context mContext;
	Button takePhoto;
	boolean flag = true;
	boolean initiatedFlag = false;
	static int picNo=0;
	private ArrayList<Bitmap> photos = new ArrayList<Bitmap>();
	@Override
	public void onCreate(Bundle savedInstanceState) {
		if(ImpoundTab.impoundState == 0)
		{	
			if(flag)
			{
				flag = false;
				Intent takePic = new Intent(ShowPhotos.this, TakePhotos.class);
				startActivity(takePic);
			}else
			{
				showDetail();
			}
		}
		else 
		{
			showDetail();
		}
		super.onCreate(savedInstanceState);
		
		initiatedFlag = true;
	}
	public void finish(){
		mContext = null;
		takePhoto = null;
		photos = null;
		super.finish();
	}
	

	 @Override
	protected void onRestart(){
		 super.onRestart(); 
		 showDetail();
	 }

	    
	private void showDetail() {
		photos.clear();
		setContentView(R.layout.showphotos);
		mContext = this;
		
		ImageAdapter imageAdapter = new ImageAdapter(mContext); 

	
		
		if(ImpoundTab.impoundState==2)
		{
			String picname;
			int i =1;

				while(i<=ImpoundTab.LIMIT){
				picname= AIMCaseConstants.SAVEDDIRECTORY+ImpoundTab.no+"img"+i+".jpg";
				File nFile = new File(picname);
		    if(nFile.exists()){
		    	if(!initiatedFlag)
		    	picNo++;
			Bitmap myBitmap = BitmapFactory.decodeFile(picname);
			photos.add(myBitmap);
			}
		    i++;
		}

		GridView gridView = (GridView) findViewById(R.id.gridView1);
//		gridView.setAdapter(new ImageAdapter(this));
		gridView.setAdapter(imageAdapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView parent, View v, int position,
					long id) {
				if (id == 0) {						
					Log.e(id+"", id+"");
				} else if (id == 1) {
					Log.e(id+"", id+"");
				} else if (id == 2) {
					Log.e(id+"", id+"");
				}
			}
		});
		}
		else
		{
			String picname;
			int i =1;
			Bitmap myBitmap = null;
//			while(i<=ImpoundTab.picLimit){
				while(i<=ImpoundTab.LIMIT){
				picname= AIMCaseConstants.SAVEDDIRECTORY
				+ AIMCaseConstants.QUICKIMAGENAME+i+".jpg";
				File nFile = new File(picname);
		    
		    if(nFile.exists()){
//			myBitmap = BitmapFactory.decodeFile(picname)
			myBitmap = decodeFile(nFile);
			photos.add(myBitmap);
//			myBitmap = null;
			}

		    i++;
			}
	
			GridView gridView = (GridView) findViewById(R.id.gridView1);
	//		gridView.setAdapter(new ImageAdapter(this));
			gridView.setAdapter(imageAdapter);
			gridView.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView parent, View v, int position,
						long id) {
					if (id == 0) {
						Log.e(id+"", id+"");
					} else if (id == 1) {
						Log.e(id+"", id+"");
					} else if (id == 2) {
						Log.e(id+"", id+"");
					}
				}
			});
		}
		
		takePhoto = (Button) findViewById(R.id.Button01);
		takePhoto.setOnClickListener(new OnClickListener(){ public void onClick(View v){
			

				if(picNo<ImpoundTab.LIMIT){
					Intent take = new Intent(
					ShowPhotos.this,
					TakePhotos.class);
					startActivity(take);
				}
			else{
				Toast.makeText(mContext, " U can save "+ImpoundTab.LIMIT+ " pictures for one case at most",
		                Toast.LENGTH_LONG).show();
				
			}
			
			}});

	}
	private Bitmap decodeFile(File f){
	    try {
	        //Decode image size
	        BitmapFactory.Options o = new BitmapFactory.Options();
	        o.inJustDecodeBounds = true;
	        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

	        //The new size we want to scale to
	        final int REQUIRED_SIZE=70;

	        //Find the correct scale value. It should be the power of 2.
	        int width_tmp=o.outWidth, height_tmp=o.outHeight;
	        int scale=1;
	        while(true){
	            if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
	                break;
	            width_tmp/=2;
	            height_tmp/=2;
	            scale*=2;
	        }

	        //Decode with inSampleSize
	        BitmapFactory.Options o2 = new BitmapFactory.Options();
	        o2.inSampleSize=scale;
	        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
	    } catch (FileNotFoundException e) {}
	    return null;
	}
	 	 
public class ImageAdapter extends BaseAdapter {
	private Context context;
	private LayoutInflater inflater;
	public ImageAdapter(Context c) {
		context = c;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}

	public int getCount() {
		return photos.size();
	}

	public Object getItem(int position) {
		return photos.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

        
		ViewHolder holder;
		ImageView imageView;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.photolayout, null);
			holder = new ViewHolder(context);
			holder.logo  = (ImageView) convertView
					.findViewById(R.id.imageView1);
			holder.delete = (ImageView) convertView.findViewById(R.id.imageView2);
			holder.t = (TextView) convertView.findViewById(R.id.textView1);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();

		}
			int number= position+1;
			holder.number = number;
			holder.logo.setImageBitmap(photos.get(position));
//			holder.logo.setPadding(5,5,5,5);
			holder.logo.setAdjustViewBounds(true);
//	        holder.logo.setMaxHeight(150);
			Display display = getWindowManager().getDefaultDisplay(); 
			int width = display.getWidth();
//			holder.logo.setMaxWidth(width/2);

	        holder.t.setText("pic:"+number);
	        holder.setClick();

		return convertView;
	

	}
}
class ViewHolder extends View{
	int number;
	ImageView logo;
	ImageView delete;
	TextView t;
	public ViewHolder(Context context) {
		super(context);
			
	}
	public void setClick(){
		delete.setOnClickListener(new OnClickListener(){ public void onClick(View v){
			if(ImpoundTab.impoundState==2)
			{
				
				File d = new File(AIMCaseConstants.SAVEDDIRECTORY
						+ ImpoundTab.no+"img"+number+".jpg");
				
				d.delete();
				
				while(number<picNo)
				{
					number++;
					File change = new File(AIMCaseConstants.SAVEDDIRECTORY
						+ ImpoundTab.no+"img"+number+".jpg");
					int n = number-1;
					
					File to = new File(AIMCaseConstants.SAVEDDIRECTORY
							+ ImpoundTab.no+"img"+n+".jpg");
					change.renameTo(to);
				}
			}else
			
			{
				File d = new File(AIMCaseConstants.SAVEDDIRECTORY
						+ AIMCaseConstants.QUICKIMAGENAME+number+".jpg");
				
				d.delete();
			
				while(number<ImpoundTab.LIMIT)
				{
					number++;
					File change = new File(AIMCaseConstants.SAVEDDIRECTORY
						+ AIMCaseConstants.QUICKIMAGENAME+number+".jpg");
					int n = number-1;
					
					File to = new File(AIMCaseConstants.SAVEDDIRECTORY
							+ AIMCaseConstants.QUICKIMAGENAME+n+".jpg");
					change.renameTo(to);
				}
			}
			
			
			
//			uploadImage();
			picNo--;
			ShowPhotos.this.onRestart();
			}
		});
	}
	
}

static public void setPhotos() {
	ImpoundTab.picNo = picNo;
}

}